/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-unused-vars */

/**
 * 计算两个时间相差的年、月、日、时、分、秒
 *
 * https://juejin.cn/post/7153816601156911118
 */
export const timeDiff = (oldTime, newTime) => {
  oldTime = oldTime.replace(new RegExp("-", "gm"), "/");
  if (newTime) {
    newTime = newTime.replace(new RegExp("-", "gm"), "/");
  } else {
    newTime = new Date();
  }

  // 计算比较日期
  const getMaxMinDate = (time, twoTime, type) => {
    const minTime =
      new Date(time).getTime() - new Date(twoTime).getTime() > 0
        ? twoTime
        : time;
    const maxTime =
      new Date(time).getTime() - new Date(twoTime).getTime() > 0
        ? time
        : twoTime;
    const maxDateDay = new Date(
      new Date(maxTime).getFullYear(),
      new Date(maxTime).getMonth() + 1,
      0
    ).getDate();
    const maxMinDate =
      new Date(minTime).getDate() > maxDateDay
        ? maxDateDay
        : new Date(minTime).getDate();
    let maxMinTong;
    if (type === "month") {
      maxMinTong =
        new Date(maxTime).getFullYear() +
        "/" +
        (new Date(minTime).getMonth() + 1) +
        "/" +
        maxMinDate +
        " " +
        new Date(minTime).toLocaleTimeString("chinese", { hour12: false });
    } else {
      maxMinTong =
        new Date(maxTime).getFullYear() +
        "/" +
        (new Date(maxTime).getMonth() + 1) +
        "/" +
        maxMinDate +
        " " +
        new Date(minTime).toLocaleTimeString("chinese", { hour12: false });
    }
    return {
      minTime,
      maxTime,
      maxMinTong,
    };
  };

  // 相差年份
  const getYear = (time, twoTime) => {
    const oneYear = new Date(time).getFullYear();
    const twoYear = new Date(twoTime).getFullYear();
    const { minTime, maxTime, maxMinTong } = getMaxMinDate(
      time,
      twoTime,
      "month"
    );
    let chaYear = Math.abs(oneYear - twoYear);
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      chaYear--;
    }
    return chaYear;
  };

  // 相差月份
  const getMonth = (time, twoTime, value) => {
    const oneMonth =
      new Date(time).getFullYear() * 12 + (new Date(time).getMonth() + 1);
    const twoMonth =
      new Date(twoTime).getFullYear() * 12 + (new Date(twoTime).getMonth() + 1);
    const { minTime, maxTime, maxMinTong } = getMaxMinDate(
      time,
      twoTime,
      "day"
    );
    let chaMonth = Math.abs(oneMonth - twoMonth);
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      chaMonth--;
    }
    if (value) {
      return chaMonth - value;
    } else {
      return chaMonth;
    }
  };

  // 相差天数
  const getDay = (time, twoTime, value) => {
    const chaTime = Math.abs(
      new Date(time).getTime() - new Date(twoTime).getTime()
    );
    if (value) {
      return parseInt(chaTime / 86400000) - value;
    } else {
      return parseInt(chaTime / 86400000);
    }
  };

  // 相差小时
  const getHour = (time, twoTime, value) => {
    const chaTime = Math.abs(
      new Date(time).getTime() - new Date(twoTime).getTime()
    );
    if (value) {
      return parseInt(chaTime / 3600000) - value;
    } else {
      return parseInt(chaTime / 3600000);
    }
  };

  // 相差分钟
  const getMinute = (time, twoTime, value) => {
    const chaTime = Math.abs(
      new Date(time).getTime() - new Date(twoTime).getTime()
    );
    if (value) {
      return parseInt(chaTime / 60000) - value;
    } else {
      return parseInt(chaTime / 60000);
    }
  };

  // 相差秒
  const getSecond = (time, twoTime, value) => {
    const chaTime = Math.abs(
      new Date(time).getTime() - new Date(twoTime).getTime()
    );
    if (value) {
      return parseInt(chaTime / 1000) - value;
    } else {
      return parseInt(chaTime / 1000);
    }
  };

  // 相差年月日时分秒
  const getDiffYMDHMS = (time, twoTime) => {
    const { minTime, maxTime, maxMinTong } = getMaxMinDate(
      time,
      twoTime,
      "day"
    );
    let diffDay1 = getDay(minTime, maxMinTong);
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      const prevMonth = new Date(maxMinTong).getMonth() - 1;
      const lastTime = new Date(maxMinTong).setMonth(prevMonth);
      diffDay1 =
        diffDay1 -
        getDay(
          new Date(lastTime).getFullYear() +
            "/" +
            (new Date(lastTime).getMonth() + 1) +
            "/" +
            new Date(lastTime).getDate(),
          maxMinTong
        );
    }
    const diffYear = getYear(time, twoTime);
    const diffMonth = getMonth(time, twoTime, diffYear * 12);
    const diffDay = getDay(time, twoTime, diffDay1);
    const diffHour = getHour(time, twoTime, getDay(time, twoTime) * 24);
    const diffMinute = getMinute(
      time,
      twoTime,
      getDay(time, twoTime) * 24 * 60 + diffHour * 60
    );
    const diffSecond = getSecond(
      time,
      twoTime,
      getDay(time, twoTime) * 24 * 60 * 60 +
        diffHour * 60 * 60 +
        diffMinute * 60
    );
    return {
      diffYear,
      diffMonth,
      diffDay,
      diffHour,
      diffMinute,
      diffSecond,
    };
  };

  return getDiffYMDHMS(oldTime, newTime);
};
